package com.example.zsfinacialmanage.activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.db.MyDBHelper;
import com.example.zsfinacialmanage.view.LoginActivity;
import com.example.zsfinacialmanage.view.MainActivity;
public class SysSettingActivity extends AppCompatActivity {
    private TextView txt_user;
    private EditText et_xpwd,et_zxpwd,et_ypwd;
    private Button btn_modify,btn_cancel;
    private MyDBHelper mhelper;
    private SQLiteDatabase db;
    private String name,pwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sys_setting);
        initView();
        displayInfo();
        btnModify();
        btncancel();
    }
    private void initView(){
        txt_user= findViewById(R.id.txt_name_sys);
        et_ypwd= findViewById(R.id.et_ypwd_sys);
        et_xpwd= findViewById(R.id.et_xpwd_sys);
        et_zxpwd= findViewById(R.id.et_zxpwd_sys);
        btn_modify= findViewById(R.id.bt_modify_sys);
        btn_cancel= findViewById(R.id.bt_cancel_sys);
        mhelper=new MyDBHelper(SysSettingActivity.this);
        db=mhelper.getWritableDatabase();
    }
    private void displayInfo(){
        name=getSharedPreferences("userinfo",MODE_PRIVATE).getString("username","");
        pwd=getSharedPreferences("userinfo",MODE_PRIVATE).getString("userpwd","");
        txt_user.setText(name);
    }
    private void btnModify(){
        btn_modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ypwd = et_ypwd.getText().toString();
                String xpwd = et_xpwd.getText().toString();
                String zxpwd = et_zxpwd.getText().toString();
                if (ypwd.equals("")) {
                    Toast.makeText(SysSettingActivity.this, "请输入原始密码", Toast.LENGTH_SHORT).show();
                }else if (!ypwd.equalsIgnoreCase(pwd)) {
                    Toast.makeText(SysSettingActivity.this, "输入的密码与原密码不一致", Toast.LENGTH_SHORT).show();
                }else if (xpwd.equals("")) {
                    Toast.makeText(SysSettingActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                }else if(xpwd.equalsIgnoreCase(ypwd)){
                    Toast.makeText(SysSettingActivity.this, "新密码不能与原密码相同", Toast.LENGTH_SHORT).show();
                }else if (zxpwd.equals("")) {
                    Toast.makeText(SysSettingActivity.this, "请再次输入新密码", Toast.LENGTH_SHORT).show();
                }else if (!zxpwd.equalsIgnoreCase(xpwd)){
                    Toast.makeText(SysSettingActivity.this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
                }else{
                    ContentValues values=new ContentValues();
                    values.put("pwd",xpwd);
                    db.update("tb_userinfo",values,"name=?",new String[]{name});
                    Toast.makeText(SysSettingActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(SysSettingActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }

            }
        });
    }
    private void btncancel(){
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(SysSettingActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}
